package cmich.cps.scheduler.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import cmich.cps.scheduler.SchedulingSystem;
import cmich.cps.scheduler.data.AssignedCourse;
import cmich.cps.scheduler.data.Schedule;

/**
 * The Class AssignedCourseController.
 * 
 * @author Jonathan Kissinger <kissi1j@cmich.edu>
 */
public class AssignedCourseController implements Controller {
	
	/** The table. */
	private final JTable table;
	
	/** The model. */
	private DefaultTableModel model;
	
	/** The schedule. */
	private Schedule schedule;
	
	/** The tab name. */
	private String tabName;

	/**
	 * Instantiates a new assigned course controller.
	 *
	 * @param table the table
	 * @param schedule the schedule
	 */
	AssignedCourseController(JTable table, Schedule schedule) {
		this.table = table;
		this.schedule = schedule;
		this.tabName = "Schedule - " + schedule.getId();
		String[] headers = { "Designator", "Number", "Section Number", "Professor", "Room", "Timeslot", "Enrollment", "Lab" };
		this.model = new DefaultTableModel(new Object[][] {}, headers ) {
			private static final long serialVersionUID = 1L;
			@Override
			public boolean isCellEditable(int rowIndex, int mColIndex) {
				return false;
			}
		};
		this.table.setModel(this.model);
		this.table.setAutoCreateRowSorter(true);
		this.table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.table.getTableHeader().setReorderingAllowed(false);
	}

	/* (non-Javadoc)
	 * @see cmich.cps.scheduler.gui.Controller#populate()
	 */
	public void populate() {
		model.setRowCount(0);
		for(AssignedCourse c : schedule.getCourses()) {
			model.addRow(new Object[] { c.getName().split("\\s")[0], c.getName().split("\\s")[1], c.getSectionNumber(), c.getProfessor(), c.getRoom(), c.getTimeslot(), c.getEnrollment(), c.getLab()});
		}
	}

	/* (non-Javadoc)
	 * @see cmich.cps.scheduler.gui.Controller#addFunctionality()
	 */
	public void addFunctionality() {
		table.addMouseListener(new TableMenu());
		table.getParent().addMouseListener(new TableMenu());
	}

	/**
	 * @author Jonathan Kissinger <kissi1j@cmich.edu>
	 * 
	 * The Class TableMenu.
	 */
	private class TableMenu extends MouseAdapter {
		
		/* (non-Javadoc)
		 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
		 */
		@Override
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				JPopupMenu popup = new JPopupMenu();
				JMenuItem printItem = new JMenuItem("Print this schedule");
				printItem.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						try {
							table.print();
						} catch (PrinterException ex) {
							String msg = "Unable to print: " + ex.toString();
							Prompts.getInstance().nonFatalError(msg);
						}
					}
				});
				JMenuItem exportTSVItem = new JMenuItem("Export this schedule to spreadsheet");
				exportTSVItem.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						SchedulingSystem.getInstance().exportTableToTSV(table, schedule);
					}
				});
				JMenuItem exportHTMLItem = new JMenuItem("Export this schedule to html");
				exportHTMLItem.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						SchedulingSystem.getInstance().exportScheduleToHTML(table, schedule);
					}
				});
				JMenuItem closeItem = new JMenuItem("Close this tab");
				closeItem.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						Frame.getInstance().closeTab(tabName);
					}
				});
				popup.add(printItem);
				popup.add(exportTSVItem);
				popup.add(exportHTMLItem);
				popup.add(closeItem);
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}
}